<?php
require_once 'init.inc';
require_once 'cf/image.inc';

function isAttributeNullForAll($iAttr) {
	global $attributes;
	foreach ($attributes as $attrsForOption) {
		if (!$attrsForOption[1][$iAttr]->isNull()) {
			return false;
		}
	}
	return true;
}

function remAttributeInAll($iAttr) {
	global $attributes;
	for ($i=0; $i<count($attributes); ++$i) {
		array_splice($attributes[$i][1], $iAttr, 1);
	}
}

function remNullAttributes(){
	global $attributes;
	if (empty($attributes)) {
		return;
	}
	$numOfAttrs = count($attributes[0][1]);
	for ($i=0; $i<$numOfAttrs; ++$i) {
		if (isAttributeNullForAll($i)) {
			remAttributeInAll($i);
			--$i;
			$numOfAttrs = count($attributes[0][1]);
		}
	}
}

function fillAttributes($product,$options,$groupId) {
	$attributes = array();
	$attributesOb = 
		$groupId ? 
			new ProductGroupAttributes($product->id(), $groupId) :
			new ProductAttributes($product->id());
			
	if (!$attributesOb->allNull()) {
		$attributes[] = array( $product->name(), $attributesOb->toArray() );		//treat product like option
	}
	
	foreach ($options as $option) {
		$attributesOb = 
			$groupId ? 
				new ProductOptionUniqueGroupAttributes($option->id(), $groupId) :
				new ProductOptionUniqueAttributes($option->id()); 
		if (!$attributesOb->allNull()){
			$attributes[] = array( $option->name(), $attributesOb->toArray() );
		}
	}
	
	remNullAttributes();
	
	return $attributes;
}

function getImage($prod,$width,$height) {
	$images = $prod->images();
	$thumb = new Thumbnail($images[0], $width, $height);
	return $thumb->toArray();
}

try
{
	$product = new Product($par->get('productId'));

	$groupId = null;
	if ($par->exist('groupId')) {
		$groupId = $par->get('groupId');
	}
	else {
		$productGroups = new ProductGroups($product->id());
		if (!$productGroups->is_empty()) {
			$groupId = $productGroups->getIterator()->current()->id();
		}
	}

	if ($groupId) {
		addGroupToPath($groupId);
	}
	
	$tplName = 'product';

	if (Group::toID('boatandmotor')==$groupId) {		//   Комплект Лодка+Мотор  606+565     707
		$ids = explode('+',trim(strip_tags($product->fullDescr())));
		$p = new Product($ids[0]);
		$m = new Manufacturer($p->manufacturerId());
		$boat = array(
			'product' => $p,
			'manufacturer' => $m,
			'name' => $p->name().' '.$m->name().' '.$p->model(),
			'image' => array(getImage($p,120,150),getImage($p,125,75)),
			'attributes' => fillAttributes($p,array(),null) 
		);
		
		$p = new Product($ids[1]);
		$m = new Manufacturer($p->manufacturerId());
		$motor = array(
			'product' => $p,
			'manufacturer' => $m,
			'name' => $p->name().' '.$m->name().' '.$p->model(),
			'image' => array(getImage($p,120,150),getImage($p,125,75)),
			'attributes' => fillAttributes($p,array(),null)
		);
		assignTemplateVars('boat','motor');
		$tplName = 'boatandmotor';
	}


	$manufacturer = new Manufacturer($product->manufacturerId());

	$optionsOb = new ProductOptions($product->id());
	$hasOptions = !$optionsOb->is_empty();
	$options = $optionsOb->toArray();

	$name = $title = $product->name().' '.$manufacturer->name().' '.$product->model();
	$description = $product->name().' '.$manufacturer->name().' '.$product->model();
	$keywords = 
		$manufacturer->name().','. 
		$product->name().' '.$product->model().',' .
		$product->name().' '.$manufacturer->name().' '.$product->model();
		
	if ($hasOptions) {
		foreach ($options as $option) {
			$keywords .= ',' . $product->name().' '.$product->model().' '.$option->name();
		}
	}


	$attributes = array();  //array(option name, array(attributes))

	if ($groupId) 
	{
      	$group = new Group($groupId);
		$path[] = array($group->name(), 'group.php?groupId='.$group->id());
		$attributes = fillAttributes($product,$options,$group->id());
	}
	else {
		$attributes = fillAttributes($product,$options,null);
	}
	
	$optionsHaveAttributes = count($attributes) > 1;

	$attrNames = array();
	if (!empty($attributes)) 
	{
		foreach ($attributes[0][1] as $attr) {
			$attrNames[] = $attr->name();
		}
	}

	$mainProductImages = array();
	$images = $product->images();

	$first = true;
	foreach ($images as $img) {
		$thumb = $first ? new Thumbnail($img, 250, 150) : new Thumbnail($img, 125, 75);
		$mainProductImages[] = $thumb->toArray();
		$first = false;
	}
	
	$isNew = $product->isInGroup('new');
   


	$relatedProducts = new RelatedProducts($product->id());
	$products = $relatedProducts->getList();

	$manufacturers = array();
	$newProducts = array();
	$productImages = array();
	foreach ($products as $p) {
		$m = new Manufacturer($p->manufacturerId());
		$manufacturers[] = $m;
		$newProducts[] = $p->isInGroup('new');
		if (count($product->images())) 
		{
			$imgs = $p->images();
			$productImages[] = $imgs[0];
		}
		else {
			$productImages[] = null;
		}
	}
   
   
	assignTemplateVars(
		'name',
		'title',
		'description',
		'product',
		'manufacturer',
		'hasOptions',
		'options',
		'optionsHaveAttributes',
		'mainProductImages',
		'attrNames',
		'attributes',
		'keywords',
		'products',
		'productImages',
		'manufacturers',
		'isNew'
	);

	display($tplName);
}
catch (Exception $e){
	header('Location: error.php');
}
?>